package com.yyy.instagram.common.PullRefreshHeader;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;

import com.yyy.instagram.R;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created by qqqqqqq on 2016/9/28.
 */

public class PullRefreshHeader extends FrameLayout implements PtrUIHandler {
    PreRefreshView preRefreshView;
    View header;

    public PullRefreshHeader(Context context) {
        super(context);
        initView();
    }

    public PullRefreshHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public PullRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    protected void initView(){
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,200);
        preRefreshView = new PreRefreshView(getContext());
        preRefreshView.setLayoutParams(params);
        preRefreshView.setMinimumWidth(getWidth());
        preRefreshView.setMinimumHeight(200);
        this.addView(preRefreshView);


        header = LayoutInflater.from(getContext()).inflate(R.layout.layout_pull_refresh_view,null);
        header.setVisibility(View.INVISIBLE);
        this.addView(header);

        ImageView image = (ImageView) header.findViewById(R.id.image);
        ObjectAnimator animator = ObjectAnimator.ofFloat(image,"rotation",0,360);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(1000);
        animator.setRepeatCount(-1);
        animator.start();
        preRefreshView.setVisibility(VISIBLE);

        FrameLayout.LayoutParams lineParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1);
        lineParams.topMargin = 199;
        ImageView imageView = new ImageView(getContext());
        imageView.setBackgroundColor(getResources().getColor(R.color.grayqian));
        imageView.setLayoutParams(lineParams);
        this.addView(imageView);
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {
        header.setVisibility(View.INVISIBLE);
        preRefreshView.setVisibility(VISIBLE);
        preRefreshView.setProgress(0);
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        float max = frame.getOffsetToRefresh();
        preRefreshView.max = max;
        preRefreshView.setProgress(0);
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        header.setVisibility(View.VISIBLE);
        preRefreshView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {

    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        final int mOffsetToRefresh = frame.getOffsetToRefresh();

        final int currentPos = ptrIndicator.getCurrentPosY();
        final int lastPos = ptrIndicator.getLastPosY();
        if(currentPos < mOffsetToRefresh){
            preRefreshView.setProgress(currentPos);
        }else{
            preRefreshView.setProgress(mOffsetToRefresh);
        }

//        if (currentPos < mOffsetToRefresh && lastPos >= mOffsetToRefresh) {
//            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
//
//            }
//        } else if (currentPos > mOffsetToRefresh && lastPos <= mOffsetToRefresh) {
//            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
//
//            }
//        }
//        switch (status){
//            case PtrFrameLayout.PTR_STATUS_INIT:{
//                Log.d("PullRefreshHeader", ptrIndicator.toString());
//                break;
//            }
//            case PtrFrameLayout.PTR_STATUS_PREPARE:{
//                Log.d("PullRefreshHeader","prepare status");
//                break;
//            }
//            case PtrFrameLayout.PTR_STATUS_LOADING:{
//                Log.d("PullRefreshHeader","loading status");
//                break;
//            }
//        }
    }
}
